import { IOFileApi } from "../network/api";
import Call from "../network/call";

/** 可操作存储库展示数据模型 */
export interface OperableStoreShow {
    /** 存储库名称 */
    Name: string;
    /** 描述备注 */
    DescriptionRemarks: string;
}

/**
 * 0:   执行成功
 * 1:   参数错误
 * 2:   意外的异常
 * 3:   执行逻辑错误
 */
export type ResultStatueCode = 0 | 1 | 2 | 3;
export function ResultStatueCodeToText(code: ResultStatueCode): string {
    switch (code) {
        case 0: return "执行成功";
        case 1: return "参数错误";
        case 2: return "意外的异常";
        case 3: return "执行逻辑错误";
        default: return `<未知的代码:(${code})>`;
    }
}

/** 响应结果模板 */
export interface Result
{
    /** 执行状态代码 (等于`0`为正常) */
    Code: ResultStatueCode;
    /** 执行结果描述 */
    Message: string;
}

/** 响应结果模板带泛型结果 */
export interface ResultType<T> extends Result
{
    /** 数据结果 */
    Data: T;
}

/** 键值对配置 */
export interface KVPair {
    [key: string]: any;
}

/**
 * 键值对自定义数据源
 * @template T 数据值的类型
 */
export default class KeyValuePairsDb {
    /**
     * 获取可操作存储区名单
     * @returns 存储区名单
     */
    public async GetOperableStores(): Promise<OperableStoreShow[]> {
        let apiItem = await IOFileApi.KeyValuePairsDb.GetOperableStores();
        let response = await Call(apiItem);
        if (response.IsError) {
            $.messager.alert({
                title: "获取可操作存储区名单出错",
                icon: "error",
                msg: response.ErrorMessage,
            });
            return null;
        }
        return response.Data as OperableStoreShow[];
    }

    /**
     * 写入键值对数据
     * @param root 数据区域
     * @param kvPairs 存储键值对
     * @returns 执行结果, 处理成功的记录条数
     */
    public async Write(root: string, kvPairs: KVPair): Promise<ResultType<number>> {
        let apiItem = await IOFileApi.KeyValuePairsDb.Write();
        let json = {
            root: root,
            kvPairs: kvPairs,
        };
        let response = await Call(apiItem, json);
        if (response.IsError) {
            $.messager.alert({
                title: "写入键值对数据出错",
                icon: "error",
                msg: response.ErrorMessage,
            });
            return null;
        }
        let data = response.Data;
        return data as ResultType<number>;
    }

    /**
     * 读取键值对数据
     * @param root 数据区域
     * @param keyExpression 键读取表达式 (正则表达式)
     * @returns 匹配键读取表达式的键值对数据
     */
    public async Read(root: string, keyExpression: string): Promise<ResultType<KVPair>> {
        return await this.Reads(root, [ keyExpression ]);
    }
    /**
     * 读取键值对数据
     * @param root 数据区域
     * @param keyExpressions 键读取表达式列表 (正则表达式)
     * @returns 匹配键读取表达式的键值对数据
     */
    public async Reads(root: string, keyExpressions: string[]): Promise<ResultType<KVPair>> {
        let apiItem = await IOFileApi.KeyValuePairsDb.Read();
        let json = {
            root: root,
            keyExpressions: keyExpressions,
        };
        let response = await Call(apiItem, json);
        if (response.IsError) {
            $.messager.alert({
                title: "读取键值对数据错误",
                icon: "error",
                msg: response.ErrorMessage,
            });
            return null;
        }
        let data: any = response.Data;
        return data as ResultType<KVPair>;
    }
}
